//
//  XDVideoController.m
//  XDEducation
//
//  Created by apple on 2017/4/20.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "XDVideoController.h"
#import "XDVideoCell.h"
#import "XDVideoHeaderView.h"
#import "XDVideoModel.h"
#import "XDVideoPlayViewController.h"
@interface XDVideoController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong) UICollectionView *collectionView;
/** headerView */
@property(nonatomic,strong) XDVideoHeaderView *headerView;
/** 好成绩视频数据源 */
@property(nonatomic,strong) NSMutableArray <XDVideoModel *>*videoModelArr;
@end

static NSString *const cellId = @"VideoCell";
static NSString *const cellHeaderId = @"VideoHeaderView";
@implementation XDVideoController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"好成绩视频";
    [self initNetworkingWithFeeType:@0];
    
    [self.view addSubview:self.headerView];
    [self initCollectionView];
}



-(XDVideoHeaderView *)headerView{

    if (!_headerView) {
        
        _headerView = [[XDVideoHeaderView alloc]init];
        _headerView.frame = CGRectMake(0, 0, ScreenW, 50);
        WeakSelf(weakself)
        _headerView.payBlock = ^(){      //付费
            //NSLog(@"付费");
            [weakself initNetworkingWithFeeType:@1];
        };
        
        _headerView.freeBlock = ^(){     //免费
            //NSLog(@"免费");
            [weakself initNetworkingWithFeeType:@0];
        };
    }
    return _headerView;
}

/**
 初始化CollectionView
 */
-(void)initCollectionView{

    //布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    //同一行两个cell间的最小间距
    layout.minimumLineSpacing = 15;
    //最小两行之间的间距
    layout.minimumInteritemSpacing = 15;
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50, ScreenW, ScreenH - 64 - 50) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    
    
    //注册cell
    UINib *cellNib=[UINib nibWithNibName:@"XDVideoCell" bundle:nil];
    
    [_collectionView registerNib:cellNib forCellWithReuseIdentifier:cellId];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.videoModelArr.count;
}

//每一个cell是什么
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    XDVideoCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
    cell.videoModel = self.videoModelArr[indexPath.row];
//    cell.backgroundColor=[UIColor groupTableViewBackgroundColor];
    WeakSelf(weakself)
    cell.videoBlock = ^(){
       XDVideoModel *model = weakself.videoModelArr[indexPath.row];

        XDVideoPlayViewController *vc = [[XDVideoPlayViewController alloc]init];
        vc.videoModel = model;
        [weakself presentViewController:vc animated:YES completion:nil];
    };
    return cell;
    
}

//每一个分组的上左下右间距

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(15, 15, 15, 15);
    
}

//定义每一个cell的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat width = (ScreenW - 60 * ScaleWidth)/2;
    CGFloat height = 160 * ScaleHeight;
    return CGSizeMake(width, height);
    
}




/**
 视频网络请求

 @param feeType 0 表示免费  1 表示付费
 */
-(void)initNetworkingWithFeeType:(NSNumber *)feeType{    //WithRefreshStatus:(RefreshState)status{
    
    [MBProgressHUD showHUD];
    //media.do
    //[page=1 | action=videolist | vtype=2 | type=0]
    
    //vtype=0 好成绩  vtype=1好习惯  vtype=2 好素养
    NSString *url = BaseURL(@"media.do?action=videolist");
    NSDictionary *dict = @{@"type":feeType,@"vtype":@2,@"page":@1};
    [XDHttpTool get:url authCode:[AccountTool shareAccountTool].currentAccount.authCode params:dict success:^(id responseObj) {
        [MBProgressHUD hideHUD];
        NSLog(@"%@",responseObj);
        NSArray *dictArr = [responseObj objectForKey:@"dataList"];
        NSMutableArray *modelArr = [XDVideoModel mj_objectArrayWithKeyValuesArray:dictArr];
        self.videoModelArr = modelArr;
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:XDNetWorkError];
        NSLog(@"%@",error);
    }];
    
}


@end
